package com.sinosoft.brokerchannel.base.utile;

import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

public class ConfigUtil {

    private static ConfigUtil instance;

    public static final String BLANK = "";

    public static final String NULL = "null";
    private String filePath = "";
    public static final String B2B_CINFIG = "b2b_config.properties";
    public static synchronized ConfigUtil getInstance() {
        if (null == instance) {
            instance = new ConfigUtil();
        }
        return instance;
    }
    private ConfigUtil() {
        filePath = getClassLoaderPath() + "config/";
    }
    /**
     * @return 返回classes的绝对路径。
     */
    public static String getClassLoaderPath() {
        String path = "";
        URL classLoaderUrl = Thread.currentThread().getContextClassLoader().getResource("");
        if (isEmpty(classLoaderUrl)) {
            path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();//tomcat/weblogic
        } else {
            path = classLoaderUrl.getPath();
        }
        if (isNotNull(path)) {
            if (path.startsWith(File.separator) || path.startsWith("/")) {
                String systemType = getSystemType();
                if ("windows".equals(systemType))
                    path = path.substring(1);
            }
            if (!path.endsWith(File.separator) && !path.endsWith("/"))
                path += "/";
        }
        return path;
    }
    public static boolean isEmpty(Object obj) {
        return obj == null;
    }
    public static boolean isNotNull(String str) {
        return !isNull(str);
    }
    public static boolean isNull(String str) {
        if (str == null) {
            return true;
        }
        str = str.trim();
        if ((str.equals(NULL)) || (str.equals(BLANK))) {
            return true;
        }
        return false;
    }
    /**
     * 获取操作系统类型
     *
     * @return 返回操作系统类型
     */
    public static String getSystemType() {
        String result = "windows";
        if (System.getProperty("os.name").equals("Linux")) {
            result = "linux";
        } else if ("Mac OS X".equals(System.getProperty("os.name"))) {
            result = "Mac OS X";
        }
        return result;
    }
    /**
     24      *
     25      * @Title: printAllProperty
     26      * @Description: 输出所有配置信息
     27      * @param props
     28      * @return void
     29      * @throws
     30      */
      private  void printAllProperty(Properties props) {
             @SuppressWarnings("rawtypes")
             Enumeration en = props.propertyNames();
             while (en.hasMoreElements())
                 {
                    String key = (String) en.nextElement();
                     String value = props.getProperty(key);
                     System.out.println(key + " : " + value);
                }
      }
      public String getProperties(String filePath, String keyWord){
             Properties prop = null;
             String value = null;
             try {
                     // 通过Spring中的PropertiesLoaderUtils工具类进行获取
                     prop = PropertiesLoaderUtils.loadAllProperties(filePath);
                     // 根据关键字查询相应的值
                     value = prop.getProperty(keyWord);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             return value;
      }
      public  void getProperties(String filePath){
             Properties prop = null;
             try {
                     // 通过Spring中的PropertiesLoaderUtils工具类进行获取
                    prop = PropertiesLoaderUtils.loadAllProperties(filePath);
                     printAllProperty(prop);
                 } catch (Exception e) {
                     e.printStackTrace();
                }
      }
//    public static void main(String[] args) {
//        String properties_1 = getProperties_1("config/b2b_config.properties", "EBusinessID");
//        System.out.println("EBusinessID = " + properties_1);
//        getProperties_1("config/b2b_config.properties");
//    }
}
